import { useMemo } from 'react';
import type { StongTableProps } from '../../StongTable/type';
import type {
  Actions,
  GetListRequest,
  State,
  Options as UseTableOptions,
} from '../useTable';
import useTable from '../useTable';

export type AnyObject = Record<string, any>;
export type { Actions, GetListRequest, State, StongTableProps };

export type TablePropsType = 'simple' | 'normal' | 'editable';
export type TableOptions = UseTableOptions & {
  tablePropsType?: TablePropsType;
  isUsePagination?: boolean;
};

function useStongTable<T>(
  services: GetListRequest<T>,
  tableProps: StongTableProps<T>,
  tableOptions: Partial<TableOptions> = { tablePropsType: 'simple' },
): [State<T>, Actions, StongTableProps<T>] {
  const {
    tablePropsType = 'simple',
    isUsePagination = true,
    ...useTableOptions
  } = tableOptions;
  const [state, actions] = useTable(services, useTableOptions);
  const { list, pagination } = state;
  const { page: current, size: pageSize, total } = pagination;

  const tableTypeProps = useMemo(() => {
    const props: { [key in TablePropsType]: StongTableProps<T> } = {
      simple: { columns: [] },
      normal: { columns: [], isShowIndex: true, isCanSetting: true },
      editable: { columns: [], isCanEdit: true },
    };
    return props[tablePropsType];
  }, [tablePropsType]);

  const stongTableProps = {
    ...tableTypeProps,
    ...tableProps,
    dataSource: list,
    pagination: isUsePagination
      ? { current, pageSize, total }
      : (false as const),
  };
  return [state, actions, stongTableProps];
}

export default useStongTable;
